<!--
 * @Description: 
 * @Author: william
 * @Date: 2024-08-22 11:47:45
 * @LastEditors: william
 * @LastEditTime: 2025-11-14 17:16:23
-->
<script setup>
import { deviceProtocolService } from '@/service/deviceProtocolService.js'
import { deviceConnectEventService } from '@/service/deviceConnectEventService.js'
import { scheduler } from '@/utils/scheduler.js'

onLaunch(() => {
  const { token, getUserInfo, setLogOut } = useUserStore()
  const recipeStore = useRecipeStore()
  g_utils.writeLog('App Launch 系统信息', uni.getSystemInfoSync())

  // #ifdef APP-PLUS
  setTimeout(() => {
    plus.navigator.closeSplashscreen() // 关闭启动屏转动条
  }, 3000)
  // 保持屏幕常亮
  uni.setKeepScreenOn({
    keepScreenOn: true
  })
  // #endif

  if (token) {
    getUserInfo()
    // 预数据列表下载
    recipeStore.setRecipeTasteOptions()
    recipeStore.setRecipeCookStyleOptions()
    recipeStore.setRecipeWeightOptions()
  } else {
    setLogOut()
  }
  // #ifdef APP-PLUS
  g_utils.writeLog('启动APP协议服务')
  deviceProtocolService.setupNetworkServer()
  g_utils.writeLog('启动APP协议服务看门狗')
  deviceProtocolService.networkServerWatcher()
  // #endif

  if (token) {
    // startScheduler()
  }
})

const startScheduler = () => {
  setTimeout(() => {
    const time = 30 * 60 * 1000 // 每30分钟执行一次
    const { startDeviceBindScheduler } = useDeviceStore()
    const { startCipeScheduler } = useRecipeStore()
    const { startCookingScheduler } = useCookingStore()
    const { startDeviceConnectScheduler } = useDeviceConnectEventStore()
    startDeviceBindScheduler(time)
    startCipeScheduler(time)
    startCookingScheduler(time)
    startDeviceConnectScheduler(time)
    // g_utils.writeLog('启动定时任务')
    scheduler.start()
  }, 2000)
}

// onShow(() => {
//   g_utils.writeLog('App Show')
// })

// onHide(() => {
//   g_utils.writeLog('App Hide')
// })
</script>

<style lang="scss"></style>
